#coding:utf-8
import os
from common.koufuApi import KoufuApi

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
import json
import requests


def case():
    # 初始化必要的参数
    script_name = os.path.basename(__file__)
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.koufu_prod_domain + "/api/koufu"
    kou_fu_api = KoufuApi()
    mng_url = kou_fu_conf.koufu_prod_domain + ":8083/web/mng"
    mng_login = kou_fu_conf.koufu_prod_domain + ":8083/web/login"
    test_result = ""
    tf = True

    #结果判断
    tf = True
    try:
        # 后管登陆
        p = kou_fu_api.get_efuli_koufu_login_data()
        r = requests.post(mng_login, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个接口efuli_koufu_login返回数据为:", r.content)
        session = r.headers["Set-Cookie"][:43]
        kou_fu_conf.mng_headers.update({"Cookie": session + "; user=efuli"})

        # 提交充值接口
        p = kou_fu_api.get_efuli_koufu_corp_account_order_apply_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个接口efuli_koufu_corp_account_order_apply返回数据为:", r.content)
        trans_date = r.json()["trans_date"]

        # 获得订单列表,刚才创建的订单
        order_id = ""
        p = kou_fu_api.get_efuli_koufu_corp_account_order_list_data()
        r = requests.post(mng_url, data=json.dumps(p), headers=kou_fu_conf.mng_headers)
        print("这个接口efuli_koufu_corp_account_order_list返回数据为:", r.content)
        return_data_list = r.json()["return_data"]["list"]
        for one in return_data_list:
            order_id = one["orderId"]
            if trans_date in order_id:
                break

        # 订单审核通过
        p = kou_fu_api.get_efuli_koufu_corp_account_order_confirm_data(update_body={"orderId": order_id})
        r = requests.post(mng_url, data=json.dumps(p), headers=kou_fu_conf.mng_headers)
        print("这个接口efuli_koufu_corp_account_order_confirm返回数据为:", r.content)



        #获取企业发票列表信息
        p = kou_fu_api.get_efuli_koufu_Enterprise_invoice_information_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli.koufu.corp.combo.order.invoice返回的全部参数", r.content.decode("utf-8"))

        recharge_order_id = ''
        invoce_list = r.json()['return_data']['list']
        for invoce in invoce_list:
            isInvoice = invoce['isInvoice']
            recharge_order_id = invoce['orderId']
            if 'N' == isInvoice:
                break

        print("未开票的订单ID为：", recharge_order_id)

        #发起企业开票配置信息保存接口
        p = kou_fu_api.get_efuli_koufu_corp_invoice_config_save_data(update_body={"recharge_order_id":recharge_order_id})
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        test_result = r.json()
        print("这个是接口efuli.koufu.corp.invoice.config.save返回的全部参数", r.content.decode("utf-8"))

        # 发起发票开具接口
        p = kou_fu_api.get_efuli_koufu_invoice_send_data(update_body={"recharge_order_id":recharge_order_id})
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        test_result = r.json()
        print("这个是接口efuli.koufu.invoice.send返回的全部参数", r.content.decode("utf-8"))


        #具体判断

    except Exception as e:
        err_info = str(e).replace("'", "").replace("\"", "")
        test_result = "非预期的异常,脚本文件:" + script_name + "; 错误信息：" + err_info
        tf = -2

    #定义预期描述
    print(str(tf))
    expect_result = "预期结果:return_desc=success"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

case()